package com.timmy.chi.ui.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.timmy.chi.R;
import com.timmy.chi.adapter.FoodsAdapter;
import com.timmy.chi.model.beans.Foods;
import com.timmy.chi.utils.Constants;
import com.timmy.chi.utils.network.MyHttp;
import com.timmy.chi.utils.network.ResultCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单下的商品页面
 */

public class FoodsActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = FoodsActivity.class.getSimpleName();
    @BindView(R.id.foods_listview)
    ListView mFoodsListview;
    @BindView(R.id.srl_order)
    SwipeRefreshLayout mSrlOrder;
    private int mOrderID;
    private Gson mGson;
    private Foods.Order mOrder;
    private FoodsAdapter mFoodsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foods);
        ButterKnife.bind(this);
        mGson = new Gson();
        mOrderID = getIntent().getIntExtra(Constants.ORDERID, 0);


        initView();

        initData();
    }

    private void initView() {
        mFoodsAdapter = new FoodsAdapter(this);
        mFoodsListview.setAdapter(mFoodsAdapter);

        mSrlOrder.setOnRefreshListener(mOnRefreshListener);
        mFoodsListview.setOnItemClickListener(this);
    }

    private void initData() {

        MyHttp.getFood(mOrderID, new ResultCallback() {
            @Override
            public void onSuccess(String s) {
                mSrlOrder.setRefreshing(false);

                Foods foods = mGson.fromJson(s, Foods.class);
                if (foods.getResponseState() == 0) {
                    List<Foods.Order.FoodStatesBean> foodStates = foods.getOrder().getFoodStates();
                    mFoodsAdapter.setData(foodStates);
                }
            }

            @Override
            public void onError() {
                mSrlOrder.setRefreshing(false);
                Toast.makeText(FoodsActivity.this, "请检查当前网络", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initData();
        }
    };

    @OnClick({R.id.iv_back, R.id.btn_jiaoshui})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_jiaoshui:
                Toast.makeText(this, "叫水", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Foods.Order.FoodStatesBean item = mFoodsAdapter.getItem(position);
        int sid = item.getSid();
        int foodId = item.getId();



        if (sid == 2) {
            Toast.makeText(this, "该菜品正在烹饪,请耐心等候", Toast.LENGTH_SHORT).show();
            return;
        } else if (sid == 3) {
            Toast.makeText(this, "该菜品已撤销", Toast.LENGTH_SHORT).show();
            return;
        }

        // 弹窗
        repealFood(foodId);
    }

    private void repealFood(final int sid) {
        Dialog dialog = new AlertDialog.Builder(this).setTitle(null).setMessage("确定要撤销该菜品吗？")
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        MyHttp.deleteState(sid, new ResultCallback() {
                            @Override
                            public void onSuccess(String s) {
                                Toast.makeText(FoodsActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                initData();
                            }

                            @Override
                            public void onError() {
                                Toast.makeText(FoodsActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });


                    }
                }).show();
        dialog.setCanceledOnTouchOutside(false);
    }
}
